/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvp.levelup.swing2;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author WORKSPOT2
 */
public class StyleFrame extends JFrame
{
    
    public StyleFrame()
    {
        StylePanel panel = new StylePanel();
        add(panel);
        setSize(200, 200);
        addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e)
            {
                
            }

            @Override
            public void windowClosing(WindowEvent e)
            {
                Toolkit t = Toolkit.getDefaultToolkit();
                t.beep();
            }

            @Override
            public void windowClosed(WindowEvent e)
            {
                
            }

            @Override
            public void windowIconified(WindowEvent e)
            {
                
            }

            @Override
            public void windowDeiconified(WindowEvent e)
            {
                
            }

            @Override
            public void windowActivated(WindowEvent e)
            {
                
            }

            @Override
            public void windowDeactivated(WindowEvent e)
            {
                
            }
        });
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    class StylePanel extends JPanel
    {
        private JLabel label; 
        public StylePanel()
        {
            StyleActionListener styleListener = new StyleActionListener();
            label = new JLabel("");
            add(label);
            JButton motif = new JButton("Motif");
            motif.addFocusListener(new FocusListener() {

                @Override
                public void focusGained(FocusEvent e)
                {
                    label.setText("Motif button gained focus");
                }

                @Override
                public void focusLost(FocusEvent e)
                {
                    label.setText("Motif button lost focus");
                }
            });
            motif.addActionListener(styleListener);
            JButton windows = new JButton("Windows");
            windows.addFocusListener(new FocusListener() {

                @Override
                public void focusGained(FocusEvent e)
                {
                    label.setText("Windows button gained focus");
                }

                @Override
                public void focusLost(FocusEvent e)
                {
                    label.setText("Windows button lost focus");
                }
            });
            windows.addActionListener(styleListener);
            add(motif);
            add(windows);
        }
    }
    
    class StyleActionListener implements ActionListener
    {
        
        @Override
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                JButton button = (JButton) e.getSource();
                String text = button.getText();
                String styleName;
                if (text.equalsIgnoreCase("motif"))
                {
                    styleName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
                }
                else
                {
                    styleName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
                }
                UIManager.setLookAndFeel(styleName);
                SwingUtilities.updateComponentTreeUI(StyleFrame.this);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
}
